<template>

	<view ref="">

		<web-view v-if='url' ref="webView" class="webViewClass" :webview-styles="webviewStyles" :src="url"
			@message="getMessage"></web-view>
		<view v-else class="webViewClass">
			<view class="app-title ac">
				<view class="fl" @click="back">
					<image class="back" src="/static/img/back.png"></image>
				</view>
				应急处置
				<!-- 	<view class="history-icon" @click="goMaintenanceHistory">
				<u-icon name="clock" size="45"></u-icon>
			</view> -->
			</view>
			<u-empty></u-empty>
		</view>
	</view>
</template>
<script>
	import {
		EMERGENCYASSESS
	} from "../../api/emergency.js"
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				webviewStyles: {
					// top: "66px",
				},
				env: {},
				landscape: false,
				faultId: "",
				armCode: "",
				processId: '',
				url: "",
				status: '',
				processStatus: "",
				almInfo: {},
				pushStatus: false,
				setTimeOutID: "",
				curWebview: '',
				timer: ''
			}
		},
		computed: {
			...mapState([
				"emerUpdataValue"
			]),
		},
		watch: {
			emerUpdataValue: {
				async handler(newVal, oldVal) {
					if (!newVal && newVal != 'refreshProcess') return
					await this.getEmergenceList('pass')

					this.pushStatus = false
				},
			}
		},
		onReady() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation("landscape-primary")

			// #endif


		},
		onLoad(options) {
			this.armCode = options.armCode
			this.faultId = options.faultId
			this.status = options.status
			this.routefrom = options.routefrom
			console.log('option', options)

		},
		onUnload() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation("portrait-primary")
			// #endif
		},
		async mounted() {

			// #ifdef H5
			window.addEventListener("message", (e) => {
				console.log('父组件收到数据了', e.data)
				//	this.passChangeNode(e.data)
				let param = e.data
				if (param.type == "back") {
					this.back()
				}
				if (param.type == "save") {
					this.passChangeNode(param.data)
				}

			})
			// #endif

			await this.getEmergenceList()
			await this.getFlow()
		},
		methods: {
			async getEmergenceList(param) {
				let params = {
					personId: this.getUserId(),
					faultId: this.faultId,
				};
				await this.fetch('GET', this.$store.state.baseEmerUrl + '/emergency/faultList', params, false).then((
					res) => {
					if (res.success && res.data) {
						this.almInfo = res.data[0]
						if (param && !this.pushStatus) {
							this.timer = new Date().getTime()
							this.url =
								`/hybrid/html/flow.html?processId=${this.processId.trim()}&timer=${this.timer}&routefrom=${this.routefrom}`
							this.getFlowNodeStatus()
						}
					} else {
						console.error(res)
					}
				})
			},
			getFlowNodeStatus() {
				// #ifdef APP-PLUS
				setTimeout(() => {
					this.curWebview = this.$scope.$getAppWebview().children()[0]
					this.curWebview.evalJS(`passInit(${JSON.stringify(this.almInfo)})`)
					// console.log('this.almInfo',JSON.stringify(this.almInfo))
				}, 500)
				// #endif
				// #ifdef H5
				setTimeout(() => {
					window.frames[0].passInit(this.almInfo)
				}, 500)
				// #endif
			},
			async getFlow(param) {
				await this.fetch('GET', this.$store.state.baseEmerUrl + '/emergency/fault-dic/' + this.armCode, {},
						false)
					.then((
						res) => {
						if (res.returnCode == "2004" && res.data) {
							this.processId = res.data.processId && res.data.processId.replace('.json', '')
							if (!this.processId) {
								this.url = ''
							} else {
								this.timer = new Date().getTime()
								this.url =
									`/hybrid/html/flow.html?processId=${this.processId.trim()}&timer=${this.timer}&routefrom=${this.routefrom}`
								this.getFlowNodeStatus()
							}

						} else {
							console.error(res)
						}
					})
			},

			//将变化的节点数据传递到后端
			passChangeNode(param) {
				console.log('sss', param)
				let params = {
					idK: this.faultId,
					processStatus: param,
				};
				this.fetch('POST', this.$store.state.baseEmerUrl + EMERGENCYASSESS.UpdataOrSaveRecord, params,
						false)
					.then((
						res) => {
						if (res.returnCode == "2004" && res.data) {
							this.pushStatus = true
						} else {
							console.error(res)
						}
					})
			},
			back() {
				clearTimeout(this.setTimeOutID)
				uni.redirectTo({
					url: "/pages/blank/index"
				})
			},
			getMessage(e) {
				console.log('接收到消息了-----', e.detail.data[0].param)
				let passValue = e.detail.data[0].param

				let node = passValue
				if (passValue.type == "back") {
					this.back()
				}
				if (passValue.type == "save") {

					this.passChangeNode(passValue.data)
				}

			},
		}
	}
</script>

<style lang="less" scoped>
	.webViewClass {
		height: calc(100vh);
		width: 100vw;
		// top: 66px !important;

	}
</style>
<style>
	* {
		/* 取消点击背景色显示蓝色 */
		-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	}
</style>